package TUILayer;
import java.util.*;
import ModelLayer.*;
import ControlLayer.*;


/**
 * Write a description of class CustomerUI here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class CustomerUI
{
    private static CustomerCtrl customerCtrl = new CustomerCtrl(); 
    private static MainMenuUI mmu = new MainMenuUI();
    
    public CustomerUI()
    {
    }

     public void startMainMenu()
    {
        boolean _exit = false;
        while (!_exit) //! means while exit not is true (that is: false)
        {
            int _choice = writeMainMenu();
            if(_choice == 1)
            {
                registerCustomer();
            }
            else{
                    if(_choice == 2)
                    {
                        showCustomer();
                    }
                    else
                    {
                        if(_choice == 3)
                        {
                            updateCustomer();
                        }
                        else
                        {
                            if(_choice == 4)
                            {
                                deleteCustomer();
                            }
                            else
                            {
                                if(_choice == 5)
                                {
                                    mmu.mainMenuStart();
                                    _exit = true;
                                }
                                else
                                {
                                    
                                }
                            }
                        }
                    }
             }
        }
    }
           
    private int writeMainMenu()
    {       
            // creates a keyboard object to read input
            Scanner keyboard = new Scanner(System.in);
            System.out.println("\f *** CUSTOMER MENU ***");
            System.out.println(" (1) Register Customer");
            System.out.println(" (2) Show Customer Information");
            System.out.println(" (3) Update Customer");
            System.out.println(" (4) Delete Customer");
            System.out.println(" (5) Return");
            System.out.print("\n Make your choice: ");
            
            int choice = keyboard.nextInt();
            
            return choice;
    }
    
    public void registerCustomer()
    {
        Scanner keyboard = new Scanner(System.in);
        int _nrPurchases = 0;
        System.out.println(" -- Register Customer --");
        System.out.println(" Customer ID: ");
        int _id =  keyboard.nextInt();
        while(customerCtrl.getCustomer(_id) != null)
        {
            System.out.println("The ID you input is already in use, please try again: ");
            _id = keyboard.nextInt();
        }
        keyboard.nextLine();
        System.out.println(" First Name: ");
        String _firstName = keyboard.nextLine();
        System.out.println(" Last Name:");
        String _lastName = keyboard.nextLine();
        System.out.println(" Bank Account Number: ");
        String _bankAccount = keyboard.nextLine();
        System.out.println(" E-Mail: ");
        String _email = keyboard.nextLine();
        System.out.println(" Phone: ");
        String _phone = keyboard.nextLine();
        System.out.println(" Address: ");
        String _address = keyboard.nextLine();
        System.out.println(" City: ");
        String _city = keyboard.nextLine();
        System.out.println(" Registrbation date: ");
        String _regDate = keyboard.nextLine();
        Customer _customer = new Customer(_id, _firstName, _lastName, _bankAccount, _email, _phone, _address, _city, _regDate, _nrPurchases);
        customerCtrl.createCustomer(_customer);
        System.out.println("Customer has been created!");
        keyboard.nextLine();
    }
    
    public void showCustomer()
    {
        Scanner keyboard = new Scanner(System.in);
        System.out.println(" -- Show Customer Information -- ");
        System.out.println(" Please input the id of the Customer: ");
        long _id = keyboard.nextInt();
        while(customerCtrl.getCustomer(_id) == null)
        {
            System.out.println("The ID you input is invalid, please try again: ");
            _id = keyboard.nextInt();
        }
        Customer _customer = customerCtrl.getCustomer(_id);
        System.out.println(" First Name: " + _customer.getFirstName());
        System.out.println(" Last Name: " + _customer.getLastName());
        System.out.println(" Bank Account Number: " + _customer.getBankAccount());
        System.out.println(" E-Mail: " + _customer.getEmail());
        System.out.println(" Phone Number: " + _customer.getPhone());
        System.out.println(" Address: " + _customer.getAddress());
        System.out.println(" City: " + _customer.getCity());
        System.out.println(" Registration Date: " + _customer.getRegDate());
        System.out.println(" Number of Purchases: " + _customer.getNrPurchases());
        customerCtrl.ifCustomerHasLeases(_id);
        keyboard.nextLine();
        keyboard.nextLine();
    }
    
    public void updateCustomer()
    {
        Scanner keyboard = new Scanner(System.in);
        int _nrPurchases = 0;
        System.out.println(" -- Update Customer --");
        System.out.println(" Customer ID: ");
        int _id =  keyboard.nextInt();
        while(customerCtrl.getCustomer(_id) == null)
        {
            System.out.println("The ID you input is invalid, please try again: ");
            _id = keyboard.nextInt();
        }
        keyboard.nextLine();
        System.out.println(" First Name: ");
        String _firstName = keyboard.nextLine();
        System.out.println(" Last Name:");
        String _lastName = keyboard.nextLine();
        System.out.println(" Bank Account Number: ");
        String _bankAccount = keyboard.nextLine();
        System.out.println(" E-Mail: ");
        String _email = keyboard.nextLine();
        System.out.println(" Phone: ");
        String _phone = keyboard.nextLine();
        System.out.println(" Address: ");
        String _address = keyboard.nextLine();
        System.out.println(" City: ");
        String _city = keyboard.nextLine();
        Customer _customer = new Customer(_id, _firstName, _lastName, _bankAccount, _email, _phone, _address, _city, customerCtrl.getCustomer(_id).getRegDate(), _nrPurchases);
        customerCtrl.updateCustomer(_customer);
        System.out.println("Customer has been updated!");
        keyboard.nextLine();
    }
    
    public void deleteCustomer()
    {
        Scanner keyboard = new Scanner(System.in);
        System.out.println(" -- Delete Customer --");
        System.out.println(" Please input the ID of the customer whom you want to remove: ");
        int _id = keyboard.nextInt();
        while(customerCtrl.getCustomer(_id) == null)
        {
            System.out.println("The ID you input is invalid, please try again: ");
            _id = keyboard.nextInt();
        }
        customerCtrl.removeCustomer(_id);
        System.out.println("Customer has been deleted!");
        keyboard.nextLine();
        keyboard.nextLine();
    }
}
